package com.blackitheima1.polymorphic.test4;
//改变多态的弊端
public class TestPolymorphic {
    public static void main(String[] args) {
        //创建对象,并调用方法
        eatAnimal(new Cat());
        eatAnimal(new Dog());

    }
    public static void eatAnimal(Animal a){
        a.eat();
        //向下转型为狗子类的特有方法
       /* Dog d= (Dog) a;
        d.watchHome();*/
       //ClassCastException 加入对象判断避免牛头不对马尾
        if(a instanceof Dog){
            Dog d= (Dog) a;
            d.watchHome();
        }
    }
}
//父类
class Animal{
    public void eat(){
        System.out.println("动物吃东西!");
    }
}
//猫子类
class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
//狗子类
class Dog extends Animal{
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }
    public void watchHome(){
        System.out.println("狗看门");
    }
}